/* Bagian 1. ADT System Modus 1 : Load, Save, AddSite, CreateTable */

/* File			: MSystemS.c									*/
/* Deskripsi	: realisasi (body) dari MSystemS.h				*/
/* Isi			: realisasi / kode program dari semua prototype	*/
/*				  yg didefinisikan pada MSystemS.h				*/

/* ADT system */

#include "MSystemS.h"

/* BODY prototype */
	void Save (string FileName, bool *OK)
		/* Modus Persiapan : Save */
		/* Mengeksekusi semua hal yang dilakukan dalam command Save sesuai dengan spek soal */
		{
			/* KAMUS LOKAL */
			/* Semua nama yang dipakai dalam BADAN PROSEDUR */
				FILE *f; int retval;
				int NumbNodes = 0, NumbEdges = 0, i, j;
				nodeAddress Q = FIRST(NODELIST); AddressMhs QMhs = FIRST(TABELMHS); AddressMK QMK = FIRST(TABELMK);
				AddressPMK QPMK = FIRST(TABELPMK); ownerAddress QOw; edgeAddress Qiu = FIRST(EDGELIST);
				string Tanggal;
		
			/* ALGORITMA */
			/* Deretan intruksi pemberian harga, input, output,
			   analisis kasus, pengulangan, atau prosedur */
				f = fopen(FileName.s, "w");
				if (f != NULL)
					{
						*OK = true;
						retval = fprintf(f, "@nodes\n");
						while (Q != NULL)
							{NumbNodes++; Q = NEXT(Q);}
						retval = fprintf(f, "%d #\n", NumbNodes);
						Q = FIRST(NODELIST);
						for (i = 1; i <= NumbNodes; i++)
							{
								retval = fprintf(f, "@@%s\n", NODENAME(Q).s);
								if (HAS_SERVER(Q))
									{
										if (HAS_CLIENT(Q))
											{retval = fprintf(f, "Y Y %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), 													  INSERTTIME(Q), UPDATETIME(Q), DELETETIME(Q));}
										else
											{retval = fprintf(f, "Y T %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), 													  INSERTTIME(Q), UPDATETIME(Q), DELETETIME(Q));}
									}
								else
									{
										if (HAS_CLIENT(Q))
											{retval = fprintf(f, "T Y %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), 													  INSERTTIME(Q), UPDATETIME(Q), DELETETIME(Q));}
										else
											{retval = fprintf(f, "T T %.4f %.4f %.4f %.4f #\n", RETRIEVETIME(Q), 													  INSERTTIME(Q), UPDATETIME(Q), DELETETIME(Q));}
									}
								if (HAS_TMHS(Q))
									{
										retval = fprintf(f, "Mahasiswa #\n");
										QMhs = FIRST(TABELMHS);
										while (QMhs != NULL)
											{
												QOw = FIRST(QMhs->_ownerList);
												while (QOw != NULL)
													{
														if (IsNodeEqual(Q, NODE(QOw)))
															{
																retval = fprintf(f, "%s ", NIM(QMhs).s);
																retval = fprintf(f, "%c", '"');
																retval = fprintf(f, "%s", NAMA(QMhs).s);
																retval = fprintf(f, "%c ", '"');
																Tanggal = DateToString(TGLLAHIR(QMhs));
																for (j = 0; j < LongStr(StringToChar(Tanggal)); j++)
																	{
																		if (Tanggal.s[j] == '/')
																			{retval = fprintf(f, "%c", '-');}
																		else
																			{retval = fprintf(f, "%c", Tanggal.s[j]);}
																	}
																retval = fprintf(f, " ");
																retval = fprintf(f, "%s ", KOTAASAL(QMhs).s);
																retval = fprintf(f, "%.2f ", IPK(QMhs));
																retval = fprintf(f, "%d ", JMLSKS(QMhs));
																retval = fprintf(f, "#\n");
															}
														QOw = QOw->next;
													}
												QMhs = QMhs->next;
											}
									}
								if (HAS_TMK(Q))
									{
										retval = fprintf(f, "MataKuliah #\n");
										QMK = FIRST(TABELMK);
										while (QMK != NULL)
											{
												QOw = FIRST(QMK->_ownerList);
												while (QOw != NULL)
													{
														if (IsNodeEqual(Q, NODE(QOw)))
															{
																retval = fprintf(f, "%s ", KODEMK(QMK).s);
																retval = fprintf(f, "%c", '"');
																retval = fprintf(f, "%s", NAMA_MK(QMK).s);
																retval = fprintf(f, "%c ", '"');
																retval = fprintf(f, "%d ", SKS(QMK));
																retval = fprintf(f, "%s #\n", BIDANG(QMK).s);
															}
														QOw = QOw->next;
													}
												QMK = QMK->next;
											}
									}
								if (HAS_TPMK(Q))
									{
										retval = fprintf(f, "PengambilanMK #\n");
										QPMK = FIRST(TABELPMK);
										while (QPMK != NULL)
											{
												QOw = FIRST(QPMK->_ownerList);
												while (QOw != NULL)
													{
														if (IsNodeEqual(Q, NODE(QOw)))
															{
																retval = fprintf(f, "%s ", NIM(MHS(QPMK)).s);
																retval = fprintf(f, "%s ", KODEMK(MK(QPMK)).s);
																retval = fprintf(f, "%d ", SEMESTER(QPMK));
																retval = fprintf(f, "%d-%d ", AWAL(TAHUN(QPMK)), AKHIR(TAHUN(QPMK)));
																retval = fprintf(f, "%c #\n", NILAI(QPMK));
															}
														QOw = QOw->next;
													}
												QPMK = QPMK->next;
											}
									}
								Q = NEXT(Q);
							}
						retval = fprintf(f, "@edges\n");
						while (Qiu != NULL)
							{NumbEdges++; Qiu = NEXT(Qiu);}
						retval = fprintf(f, "%d #\n", NumbEdges);
						Qiu = FIRST(EDGELIST);
						for (i = 1; i <= NumbEdges; i++)
							{
								retval = fprintf(f, "%s %s %.1f %.1f #\n", NODENAME(GetA(Qiu)).s, NODENAME(GetB(Qiu)).s, 										 GetUpload(Qiu), GetDownload(Qiu));
								Qiu = NEXT(Qiu);
							}
						retval = fprintf(f, "@end");
					}
				else
					{*OK = false;}
				fclose(f);
		}
